package com.rmdjzz.slmmn.controller.user.comment;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.rmdjzz.slmmn.pojo.base.Comment;
import com.rmdjzz.slmmn.pojo.base.User;
import com.rmdjzz.slmmn.pojo.result.Result;
import com.rmdjzz.slmmn.pojo.vo.CommentVO;
import com.rmdjzz.slmmn.service.CommentService;
import com.rmdjzz.slmmn.service.SingerService;
import com.rmdjzz.slmmn.service.SongService;
import com.rmdjzz.slmmn.service.UserService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @作者 张绍鑫
 * @创建时间 2023/6/10
 */
@RequestMapping("/user")
@Api(tags = "用户评论模块")
@Controller
public class CommentUser {
    @Autowired
    private SongService songService;

    @Autowired
    private SingerService singerService;

    @Autowired
    private CommentService commentService;

    @Autowired
    private UserService userService;

    /**
     * 根据歌曲id查询评论
     * 待转vo字段 user_id
     * 返回vo userName comment
     * @param id
     * @return
     */
    @GetMapping("/commentList/{id}")
    @ResponseBody
    public Result commentList(@PathVariable Integer id){
        QueryWrapper<Comment> qw = new QueryWrapper<>();
        qw.eq("song_id",id);
        //获取初始数据
        List<Comment> list = commentService.list(qw);
        //转化为vo数据
        ArrayList<CommentVO> commentVOS = new ArrayList<>();
        for (Comment  comment: list ){
            //单转
            CommentVO commentVO = new CommentVO();
            if(userService.getById(comment.getUserId())==null){
                commentVO.setUser("未知用户");
            }else{
                commentVO.setUser(userService.getById(comment.getUserId()).getUserName());
            }
            commentVO.setComment(comment.getComment());

            //添加
            commentVOS.add(commentVO);
        }
        return Result.ok(commentVOS);
    }

    @PostMapping("/userComment")
    @ResponseBody
    public Result userComment(Comment comm){
        //保存评论
        commentService.save(comm);
        //查询
        Integer id = comm.getSongId();
        QueryWrapper<Comment> qw = new QueryWrapper<>();
        qw.eq("song_id",id);
        //获取初始数据
        List<Comment> list = commentService.list(qw);
        //转化为vo数据
        ArrayList<CommentVO> commentVOS = new ArrayList<>();
        for (Comment  comment: list ){
            //单转
            CommentVO commentVO = new CommentVO();
            commentVO.setUser(userService.getById(comment.getUserId()).getUserName());
            commentVO.setComment(comment.getComment());

            //添加
            commentVOS.add(commentVO);
        }
        return Result.ok(commentVOS);
    }
}
